<template>
  <div class="app-container">
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item name="1">
        <template slot="title">
          <i class="el-icon-search" />搜索栏
        </template>
        <div class="head-container">
          <el-form class="searchform" :model="params" :inline="true" size="mini" label-width="auto" @keyup.enter.native="toQuery">
            <el-form-item label="变量名称">
              <el-input v-model="params.variableName" placeholder="请输入" clearable style="width: 160px;" />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" style="float:right;margin-left:2px;" icon="el-icon-refresh" @click="resetForm">重置</el-button>
              <el-button size="mini" type="primary" style="float:right;margin-left:1px;" icon="el-icon-search" @click="toQuery">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div style="padding-top:5px">
      <el-button size="mini" type="danger" style="float:right;margin-bottom:5px;margin-right:px;" icon="el-icon-remove" @click="batchDelete">删除</el-button>
      <el-button size="mini" type="success" style="float:right;margin-bottom:5px;margin-right:2px;" icon="el-icon-circle-plus" @click="toAdd">新增</el-button>
      <el-table
        ref="table"
        :data="data"
        :row-key="getRowKeys"
        stripe
        border
        size="mini"
        :height="tableHeight"
        style="width: 100%;"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
        @header-dragend="colChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="expand">
          <template slot-scope="props">
            <div class="sub_table">
              <el-table
                :data="props.row.variableFields"
                size="mini"
                stripe
                style="width: 100%"
              >
                <el-table-column sortable label="属性KEY" prop="fieldKey" />
                <el-table-column sortable label="属性名称" prop="fieldName" />
                <el-table-column :show-overflow-tooltip="true" label="属性类型" prop="dataType">
                  <template slot-scope="scope">
                    {{ showNameByValue(scope.row.dataType, dataTypes) }}
                  </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" />
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="变量KEY" prop="variableKey" :width="colWidths.variableKey" />
        <el-table-column :show-overflow-tooltip="true" label="变量名称" prop="variableName" :width="colWidths.variableName" />
        <el-table-column :show-overflow-tooltip="true" label="备注" prop="remark" :width="colWidths.remark" />
        <el-table-column :show-overflow-tooltip="true" label="创建人" prop="createdBy" :width="colWidths.createdBy" />
        <el-table-column :show-overflow-tooltip="true" label="更新人" prop="updatedBy" :width="colWidths.updatedBy" />
        <el-table-column :show-overflow-tooltip="true" label="创建时间" prop="createdTime" :width="colWidths.createdTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="更新时间" prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="operation" align="center" fixed="right" width="180">
          <template slot-scope="scope">
            <div class="list-operation">
              <el-link size="mini" type="primary" style="margin-left:0px;" @click="toView(scope.row)">
                <span style="font-size: 12px;">查看</span>
              </el-link>
              <el-divider direction="vertical" />
              <el-link size="mini" type="warning" style="margin-left:0px;" @click="toEdit(scope.row)">
                <span style="font-size: 12px;">修改</span>
              </el-link>
              <el-divider direction="vertical" />
              <el-link size="mini" type="danger" style="margin-left:0px;" @click="toDelete(scope.row)">
                <span style="font-size: 12px;">删除</span>
              </el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page.sync="currentPage"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <edit ref="edit" :parent="currentObj" />
  </div>
</template>

<script>
import initData from '@/api/coi/initData'
import { parseTime } from '@/utils/index'
import { Message } from 'element-ui'
import { showNameByValue } from '@/utils/selectUtil'
import edit from './edit'
import { deleteRuleVariableById, batchDeleteRuleVariableById } from '@/api/coi/configuration/rule-config/rule-variable'
import { findAllDataTypes } from '@/api/coi/configuration/rule-config/rule-variable'

export default {
  components: { edit },
  mixins: [initData],
  data() {
    return {
      syncTime: [],
      ids: [],
      currentObj: this,
      activeName: '1',
      tableHeight: window.innerHeight * 0.58,
      dataTypes: [],
      baseUrl: window.appConfig.coiBuyerConfigBaseUrl
    }
  },
  watch: {
    data: {
      handler(n, o) {
        this.$nextTick(() => {
          this.doLayout()
        })
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      window.addEventListener('resize', this.doLayout, true)
      this.loadDataTypes()
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.doLayout, true)
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    showNameByValue,
    loadDataTypes() {
      var _this = this
      findAllDataTypes(this.baseUrl).then(res => {
        if (res) {
          _this.dataTypes = res.result
        }
      })
    },
    doLayout() {
      if (this.$refs.table) {
        this.$refs.table.doLayout()
      }
    },
    beforeInit() {
      this.url = process.env.VUE_APP_BASE_API7 + '/ruleVariable/findPage'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    // 列表排序触发
    sortChange(val) {
      const sortCol = val.prop.replace(/([A-Z])/g, '_$1').toUpperCase()
      let sortType = ''
      if (val.order === 'descending') {
        sortType = ' DESC'
      } else if (val.order === 'ascending') {
        sortType = ' ASC'
      }
      this.params.sort = sortCol + sortType
      this.init()
    },
    toQuery() {
      this.page = 0
      this.currentPage = 1
      this.init()
    },
    toView(variable) {
      const edit = this.$refs.edit
      edit.title = '查看变量'
      edit.entity = variable
      edit.editDisabled = true
      edit.dialog = true
    },
    toAdd(variable) {
      const edit = this.$refs.edit
      edit.title = '新增变量'
      edit.editDisabled = false
      edit.dialog = true
    },
    toEdit(variable) {
      this.$refs.table.clearSelection(this.multipleSelection)
      this.$refs.table.toggleRowSelection(variable, true)

      const edit = this.$refs.edit
      var json = JSON.stringify(variable)
      var copyVariable = JSON.parse(json)
      edit.title = '编辑变量'
      edit.entity = copyVariable
      edit.editDisabled = false
      edit.dialog = true
    },
    // 查询栏重置触发
    resetForm() {
      this.param = { variableName: '' }
    },
    // 列表多选框触发
    handleSelectionChange(val) {
      this.ids = []
      val.forEach(item => {
        this.ids.push(item.id)
      })
    },
    // 翻页选择row的key值
    getRowKeys(row) {
      return row.id
    },
    toDelete(variable) {
      this.$refs.table.clearSelection(this.multipleSelection)
      this.$refs.table.toggleRowSelection(variable, true)
      var _this = this
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRuleVariableById(this.baseUrl, variable.id).then(res => {
          Message({
            message: res.result,
            type: 'success',
            duration: 2 * 1000
          })
          _this.toQuery()
        })
      })
    },
    // 批量删除
    batchDelete() {
      if (this.ids.length <= 0) {
        Message({
          message: '请选择删除项!',
          type: 'warning',
          duration: 2 * 1000
        })
        return
      }
      var _this = this
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDeleteRuleVariableById(_this.baseUrl, _this.ids).then(res => {
          Message({
            message: res.result,
            type: 'success',
            duration: 2 * 1000
          })
          _this.toQuery()
        })
      })
    }
  }
}
</script>
<style scoped>
.sub_table1 >>> .el-table_row>td{
  /* 去除表格线 */
  border: none;
}

.sub_table1 >>> .el-table th.is-leaf {
  /*去除上边框*/
  border: none;
}
.sub_table1 >>> .el-table::before{
  /* 去除下边框 */
  height: 0;
}
.list-operation >>> .el-divider--vertical {
  margin: 0 2px
}
</style>
